/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var DynaObject = new Class({
    Extends: GameObject,
    initialize: function(type){
        this.parent();
        this.type=type;
        this.symbol=this.getSymbol();
        if(!game.headless){
            this.HTMLIncarnation=new Element('div',{
                'class': type
            });
            this.leftStyleProperty=this.HTMLIncarnation.style.left;
            this.topStyleProperty=this.HTMLIncarnation.style.top;
        }
    },
    setX: function(left){
        if(this.x!=left){
            this.x=left;
            if(!game.headless)
                this.leftStyleProperty=this.x;
        }
    },
    setY: function(top){
        if(this.y!=top){
            this.y=top;
            if(!game.headless)
                this.topStyleProperty=this.y;
        }
    },
    setPosition: function(left,top){
        this.setX(left);
        this.setY(top);
        // when they come into existence they must come at precise position
        // that is x multiple of game.standardWidth and y multiple of game.standardHeight

        // for debugging
        if(game.debug==1){
            if(this.x%game.standardWidth!=0)
                alert(this.type+' x='+this.x);
            if(this.y%game.standardHeight!=0)
                alert(this.type+' y='+this.y);
        }
        this.zonifyObject();
    },
    zonifyObject: function(){
        if($defined(this.myZone)){
            var newZone = util.getZoneAtPoint(this.x,this.y);
            if(newZone != this.myZone){
                if(!newZone.freeZone){
                    if(newZone.hasType(GAMEUNITTYPE.WALL) || newZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                        if(game.debug==1)
                            alert('zone at: '+this.x+'-'+this.y+' is a wall. it has a type: '+gameZone.types());
                    }
                }
                newZone.addElement(this);
                this.freeMyZone();
                this.myZone = newZone;
            }
        } else {
            var gameZone=util.getZoneAtPoint(this.x,this.y);
            if(!gameZone.freeZone){
                if(gameZone.hasType(GAMEUNITTYPE.WALL) || gameZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                    if(game.debug==1)
                        alert('zone at: '+this.x+'-'+this.y+' is a wall. it has a type: '+gameZone.types());
                }
            }
            gameZone.addElement(this);
            this.myZone=gameZone;
        }
    },
    freeMyZone: function(){
        this.myZone.removeElement(this);
    },
    terminate: function(){
        this.freeMyZone();
        if(!game.headless){
            this.HTMLIncarnation.dispose();
            this.HTMLIncarnation.destroy();
            this.HTMLIncarnation=null;
            this.leftStyleProperty=null;
            this.topStyleProperty=null;
        }
    },
    getSymbol: function(){
        if(this.type==GAMEUNITTYPE.WALL)
            return '#';
        if(this.type==GAMEUNITTYPE.FAKEWALL)
            return '@';
        if(this.type==GAMEUNITTYPE.HERO)
            return 'P';
        return ' ';
    }
});
